/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.itintegration.catalogue.camel.processor;

import java.io.IOException;
import java.util.List;
import org.apache.camel.Exchange;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import se.sll.service.catalogue.common.rest.model.user.User;

/**
 *
 * @author khaleddaham
 */
public class UserProcessor {

    private static final UserProcessor INSTANCE = new UserProcessor();

    private final ObjectMapper mapper = new ObjectMapper();

    private UserProcessor() {
    }

    public static UserProcessor getInstance() {
        return INSTANCE;
    }

    /**
     *
     * @param exchange
     * @return
     * @throws IOException
     */
    private List<User> convertJsonToUser(Exchange exchange) throws IOException {
        return mapper.readValue(exchange.getIn().getBody(String.class), new TypeReference<List<User>>(){});
    }

    /**
     *
     * @param exchange
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public void userResponseJSON(Exchange exchange) throws JsonParseException, JsonMappingException, IOException {
        exchange.getIn().setBody(convertJsonToUser(exchange));
    }

}
